<?php
namespace app\index\controller;
use app\common\model\ForumNoteCommentModel;
use think\Controller;
use app\common\model\CenterModel;
use app\common\model\ForumNoteModel;
use app\common\model\ForumCollectModel;


class Center extends Base
{
    public function __construct (){
        parent::__construct();
        $this->isLogin();

    }


    /**
     * 个人主页
     */
    public function home(){
        $params = request()->param();
        if( !empty($params['cid']) ){
            $where['id'] = $params['cid'];
            $noteWhere['center_id'] = $params['cid'];
        }else{
            $where['id'] = CID;
            $noteWhere['center_id'] = CID;
        }
        $center = new CenterModel();
        $center_info =  $center->getOneCenter($where);
        $center_info = json_decode($center_info,true);

        $note = new ForumNoteModel();
        $noteList = $note->getNoteList($noteWhere);
        $comment = new ForumNoteCommentModel();
        $commentList = $comment->getNoteByComment($where['id']);

        $this->assign('info',$center_info);
        $this->assign('noteList',$noteList);
        $this->assign('commentList',$commentList);
        $this->assign('navP','home');
        return $this->fetch('home');
    }

    /**
     * 用户中心
     */
    public function index(){

        $this->assign('navP','index');
        return $this->fetch('index');
    }



    /**
     *我发表的帖
     */
    public function my_note(){
        $note = new ForumNoteModel();
        $where['center_id']= CID;
        $noteList = $note->getNoteList($where);

        $collect = new ForumCollectModel();
        $collectList = $collect->getCollect();

        $this->assign('navP','my_note');
        $this->assign('noteList',$noteList);
        $this->assign('collectList',$collectList);
        return $this->fetch('my_note');
    }



    /**
     *基本设置
     */
    public function set(){
        $params = request()->param();
        $option = '';
        if( !empty($params['option']) ){
            $option = $params['option'];
        }

        if( request()->isPost() ) {
            if($option=='info'||$option==""){
                $params['id'] = CID;
                $center = new CenterModel();
                if( !$center->updateCenter($params) ){
                    $this->error('修改失败');
                }
            }
            if($option=='pass'){
                $where = [
                    'password' =>  md5($params['nowpass'] . config('salt')),
                    'username' => $this->center_info['username']
                ];
                $center = new CenterModel();
                if (!$center->getOneCenter($where)) {
                    $this->error('旧密码错误！');
                }
                if ($params['pass'] !== $params['repass']) {
                    $this->error('两次输入的密码不一致！');
                }
                $editParam = [
                    'id' => CID,
                    'password' => md5($params['pass'] . config('salt')),
                ];
                if ( !$center->updateCenter($editParam) ) {
                    $this->error('修改失败！');
                }
            }
            $this->success('修改成功');
        }else{
            $this->assign('navP','set');
            $this->assign('option',$option);
            return $this->fetch('set');
        }

    }



    /**
     *我的消息
     */
    public function message(){

        $this->assign('navP','message');
        return $this->fetch('message');
    }


    /**
     *上传图片
     */
    public function avatar_upload()
    {
        $file = request()->file('file');
        $upload_path = ROOT_PATH . 'public' . DS .'static/index/images/avatar' ;
        $info = $file->validate(['size'=>100*1024,'ext'=>'jpg,png,gif'])->move($upload_path,CID);
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            // echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getFilename();
            $url = '/static/index/images/avatar'. DS . $info->getSaveName();
            $result = ['code' => 0, 'url' => $url];
            return json($result);
        }else{
            // 上传失败获取错误信息
            $result = ['code' => 1, 'msg' => $file->getError()];
            return json($result);
        }
    }

    /**
     *更新图片
     */
    public function updateCenterThumb(){
        $params = request()->param();
        $params['id'] = CID;
        $center = new CenterModel();
        if( !empty($params['thumb']) && $center->updateCenter($params) ) {
            $result = ['code' => 0,'msg' => '更新成功' ];
        }else{
            $result = ['code' => 1, 'msg' => '更新失败'];
        }
        return json($result);
    }


}
